<?php

/**
 * 配置文件－－主配置文件
 *
 * @package    main
 * @subpackage configure
 * @author     苏宁 <snsnsky@gmail.com>
 *
 * $Id$
 */

// 是否需要进行SESSION处理
!defined('SESSION') && define('SESSION', true);

// 框架所在路径
define('BASEPATH_FRAMEWORK', dirname(__DIR__) . '/');

// 模块所在路径
define('BASEPATH_MODULE', BASEPATH_FRAMEWORK . 'modules/');

// 模块所在路径
define('BASEPATH_CONTROL', BASEPATH_FRAMEWORK . 'controls/');

// 库所在路径
define('BASEPATH_LIBRARY', BASEPATH_FRAMEWORK . 'lib/');

// 文件缓存路径
define('BASEPATH_CACHE', BASEPATH_FRAMEWORK . 'cache/');

// 配置文件路径
define('BASEPATH_CONFIG', __DIR__ . '/');

// 静态公共文件路径
define('BASEPATH_PUBLIC', ROOT_PATH . 'public/');

// 定义HTTP协议
define("HTTP_SCHEME", $_SERVER['HTTPS'] == 'on' || $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on' ? 'https' : 'http');

// 网站起始路径
define('ROOT_URL', autohost(HTTP_SCHEME . '://www.' . CURRENT_HOST . '/'));
define('JC_URL', autohost(HTTP_SCHEME . '://demo.' . CURRENT_HOST . '/'));

// 初始化配置变量
$cfg = [
    'path' => [
        'conf' => BASEPATH_CONFIG,
        'root' => BASEPATH_FRAMEWORK,
        'lib' => BASEPATH_LIBRARY,
        'class' => BASEPATH_LIBRARY,
        'common' => BASEPATH_LIBRARY,
        'cache' => BASEPATH_CACHE,
        'upload' => BASEPATH_PUBLIC . 'upload/',
        'fonts' => BASEPATH_PUBLIC . 'fonts/',
        'temp' => BASEPATH_PUBLIC . 'temp/',
        'module' => BASEPATH_MODULE,
    ],
    'url' => [
        // 网站首页
        //
        'root' => ROOT_URL,
        'js' => JC_URL . 'public/js/',
        'css' => JC_URL . 'public/css/',
        'swf' => ROOT_URL . 'public/swf/',
        'images' => JC_URL . 'public/images/',
        'public' => ROOT_URL . 'public/',
    ],
    'cache' => [
        'root' => BASEPATH_CACHE,  // engine=memcached 时为服务器地址
        'engine' => 'file', // file|memcached
        'port' => 11211, // engine=memcached 时才有意义
        'timeout' => 60, // engine=memcached 时才有意义
    ],
    // 网站简称全称
    'site' => [
        'title' => '金果',
        'name' => '金果',
    ],
];

//页面信息
$cfg['page'] = [
    'charset' => 'UTF-8',
    'contentType' => 'text/html',
    'title' => '',
    'cached' => true,
    'engine' => 'smarty',
    'adminEngine' => 'adminSmarty',
    'css' => [],
    'js' => [],
];

// Smarty
$cfg['smarty'] = [
    'template_dir' => BASEPATH_FRAMEWORK . 'templates' . SITE_SUFFIX . '/',
    'compile_dir' => $cfg['path']['cache'] . 'smarty' . SITE_SUFFIX . '/',
];

/**
 * 自动转换测试环境下的主机名
 *
 * @param string $url
 * @return string
 */
function autohost($url)
{
    if (__ENV__ == TEST && strpos($_SERVER['HTTP_HOST'], 'test.') !== false && strpos($url,
            '://test.') === false && strpos($url, 'http') === 0) {
        return str_replace('://', '://test.', $url);
    }

    return $url;
}